﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using log4net;
using Sealight.API.Framework;
using Sealight.Core.DataAdapter;
using Sealight.UI.WebUI.UIHelpers;

namespace Sealight.UI.WebUI.Areas.Admin.Controllers.DV
{
    [Sealight.UI.WebUI.UIHelpers.SessionExpireFilter]
    [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public class DVController : Controller
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(NewsController));
        public ActionResult Index()
        {
            return View("_manage");
        }
        public ActionResult AddUpdate(string id)
        {
            service data = null;
            if(!string.IsNullOrEmpty(id))
            {
                Guid dId = Guid.Empty;

                Guid.TryParse(id, out dId);

                data = Sealight.UI.WebUI.Services.ServicesService.GetById(dId);

            }
            return View("_AddUpdateService", data);
        }
        [Authorize]
        [ValidateInput(false)]
        public ActionResult AddOrUpdate()
        {
            string title = string.Empty;
            string description = string.Empty;
            string detail = string.Empty;
            string imgUrl = string.Empty;
            bool isActive = false;

            Guid id = Guid.Empty;

            if (Request["title"] != null)
            {
                title = HttpUtility.UrlDecode(Request["title"].ToString());
            }

            if (Request["imgUrl"] != null)
            {
                imgUrl = Request["imgUrl"].ToString();
            }

            if (Request["description"] != null)
            {
                description = HttpUtility.HtmlDecode(Request["description"].ToString());
            }

            if (Request["detail"] != null)
            {
                detail = HttpUtility.HtmlDecode(Request["detail"].ToString());
            }

            if (Request["id"] != null)
            {
                try
                {
                    id = new Guid(Request["id"].ToString());
                }
                catch { }
            }

            try
            {

                isActive = bool.Parse(Request["publish"]);
            }
            catch { }

            AjaxOperationResponse response = new AjaxOperationResponse();

            service data = null;
            try
            {

                if (id != Guid.Empty)
                {
                    data = Sealight.UI.WebUI.Services.ServicesService.GetById(id);
                }

                if (data == null)
                    data = new service();

                data.title = title;
                data.description = description;
                data.detail = detail;
                data.created_date = DateTime.Now;
                data.updated_date = DateTime.Now;
                data.published_date = DateTime.Now;
                data.is_active = isActive;
                //data.language = new language { id = this.LanguageId };

                if (!string.IsNullOrEmpty(imgUrl))
                    data.image_url = imgUrl;

                Sealight.UI.WebUI.Services.ServicesService.AddUpdate(data);

            }
            catch (Exception ex)
            {
                log.Error("Error while AddOrUpdate data.", ex);
                response.OperationComplete = false;
                response.ErrorCode = "4001";
                response.ErrorFriendlyMessage = string.Format("{0}", UIHelpers.Message.Error);

                return Json(response);
            }

            return Json(data);
        }

        [Authorize]
        public ActionResult GetDisplayForAdmin()
        {
            int maxRecord = 20;
            int currentPage = 0;

            IList<service> data = null;
            Sealight.UI.WebUI.Models.ObjectPagingModel<service> viewData = new Sealight.UI.WebUI.Models.ObjectPagingModel<service>();
            try
            {
                maxRecord = int.Parse(Request["maxRecord"]);
                currentPage = int.Parse(Request["currentPage"]);
            }
            catch { }
            AjaxOperationResponse response = new AjaxOperationResponse();
            try
            {
                var listData = Sealight.UI.WebUI.Services.ServicesService.Gets();
                data = listData.Skip(maxRecord * currentPage).Take(maxRecord).ToList();
                viewData.Data = data;
                viewData.TotalPage = listData.Count / maxRecord + (listData.Count % maxRecord > 0 ? 1 : 0);
                viewData.PadingPage = CommonHelper.GetPageListView(currentPage, viewData.TotalPage);
            }
            catch (Exception ex)
            {
                log.Error("GetDisplayForAdmin::" + ex.Message);
                response.OperationComplete = false;
                response.ErrorCode = "4001";
                response.ErrorFriendlyMessage = string.Format("{0}", UIHelpers.Message.Error);

                return Json(response);
            }

            return Json(viewData);
        }

        [Authorize]
        public ActionResult GetServiceForAdmin()
        {
            Guid id = Guid.Empty;
            service data = null;
            try
            {
                if (Request["id"] != null)
                {
                    id = new Guid(Request["id"]);
                }
            }
            catch { }
            AjaxOperationResponse response = new AjaxOperationResponse();
            try
            {
                data = Sealight.UI.WebUI.Services.ServicesService.GetById(id);
            }
            catch (Exception ex)
            {
                log.Error("GetServiceForAdmin::" + ex.Message);
                response.OperationComplete = false;
                response.ErrorCode = "4001";
                response.ErrorFriendlyMessage = string.Format("{0}", UIHelpers.Message.Error);

                return Json(response);
            }
            return Json(data);
        }

        [Authorize]
        public ActionResult DeleteForAdmin()
        {
            Guid id = Guid.Empty;
            try
            {
                if (Request["id"] != null)
                {
                    id = new Guid(Request["id"]);
                }
            }
            catch { }
            AjaxOperationResponse response = new AjaxOperationResponse();
            try
            {
                if (id != Guid.Empty)
                {
                    Sealight.UI.WebUI.Services.ServicesService.Delete(id);
                }
            }
            catch (Exception ex)
            {
                log.Error("DeleteForAdmin::" + ex.Message);
                response.OperationComplete = false;
                response.ErrorCode = "4001";
                response.ErrorFriendlyMessage = string.Format("{0}", UIHelpers.Message.Error);

                return Json(response);
            }
            return Json("true");
        }
    }
}
